package com.cyam.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.ImageFilter;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomListColorGenerator;
import com.octo.captcha.component.image.deformation.ImageDeformation;
import com.octo.captcha.component.image.deformation.ImageDeformationByFilters;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
/**
 * 验证码的生成规则
 * @author CourageAQ
 *
 */
public class JCaptchaEngine extends ListImageCaptchaEngine{
	
	
	/**最小长度和最大长度设置一致时，表示验证码的长度是固定的，
	 * 否则验证码的长度在最小长度和最大长度之间随机选择
	 */
	/**设置最小长度*/
	private static final int MIN_WORD_LENGTH = 4;
	/**设置最大长度*/
	private static final int MAX_WORD_LENGTH = 4;
	/**设置验证码显示的最小字体大小*/
	private static final int MIN_FONT_SIZE = 40;
	/**设置验证码显示的最大字体大小*/
	private static final int MAX_FONT_SIZE = 50;
	/**设置验证码图片的宽度*/
	private static final int IMAGE_WIDTH = 250;
	/**设置验证码图片的高度*/
	private static final int IMAGE_HEIGHT = 100;
	

	@Override
	protected void buildInitialFactories() {
		/**
		 * 设置验证码的显示方式 String类型
		 * 纯数字模式	new RandomWordGenerator("0123456789");
		 * 当然也可以是纯字母模式、或者混合方式，甚至是中文
		 * 
		 */
		WordGenerator dictionnaryWords = new RandomWordGenerator("0123456789");
		/**
		 * 设置验证码的字符长度，以及字体颜色，
		 * 字体颜色的从设置的颜色数组中随机显示
		 */
		TextPaster randomPaster = new DecoratedRandomTextPaster(MIN_WORD_LENGTH,
				MAX_WORD_LENGTH, new RandomListColorGenerator(new Color[] {
						new Color(23, 170, 27), new Color(220, 34, 11),
						new Color(23, 67, 172) }), new TextDecorator[] {});
		/**
		 * 设置验证码（图片的宽高），同时设置图片的背景颜色
		 */
		BackgroundGenerator background = new UniColorBackgroundGenerator(
				IMAGE_WIDTH, IMAGE_HEIGHT, new Color(Integer.decode("#ECF9FF")));

		/**
		 * 设置验证码字体的大小，并设置每个字体的大小和样式
		 */
		FontGenerator font = new RandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE,
				new Font[] { new Font("nyala", Font.BOLD, MAX_FONT_SIZE),
						new Font("宋体", Font.BOLD, MAX_FONT_SIZE),
						new Font("nyala", Font.BOLD, MIN_FONT_SIZE) });
		ImageDeformation postDef = new ImageDeformationByFilters(
				new ImageFilter[] {});
		ImageDeformation backDef = new ImageDeformationByFilters(
				new ImageFilter[] {});
		ImageDeformation textDef = new ImageDeformationByFilters(
				new ImageFilter[] {});

		WordToImage word2image = new DeformedComposedWordToImage(font,
				background, randomPaster, backDef, textDef, postDef);
		addFactory(new GimpyFactory(dictionnaryWords, word2image));
	}

}
